/*
    SDL - Simple DirectMedia Layer
    Copyright (C) 1997-2009 Sam Lantinga

    This library is free software; you can redistribute it and/or
    modify it under the terms of the GNU Lesser General Public
    License as published by the Free Software Foundation; either
    version 2.1 of the License, or (at your option) any later version.

    This library is distributed in the hope that it will be useful,
    but WITHOUT ANY WARRANTY; without even the implied warranty of
    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
    Lesser General Public License for more details.

    You should have received a copy of the GNU Lesser General Public
    License along with this library; if not, write to the Free Software
    Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA  02110-1301  USA

    Sam Lantinga
    slouken@libsdl.org
*/
#include "SDL_config.h"

#ifndef _SDL_cellvideo_h
#define _SDL_cellvideo_h

#ifdef __SNC__
#define FASTCALL __fastcall
#else
#define FASTCALL
#endif

#include "SDL.h"

#include "../SDL_sysvideo.h"
#define _THIS	SDL_VideoDevice *this

#include <sys/event.h>
#include <PSGL/psgl.h>

#include "SDL_celloverlayimage.h"
#include "input/keydata.h"
#include "input/SDL_celloskdata.h"
#include "input/SDL_cellpadmap.h"

#ifdef CELL_OSK_CAPTION
#include <SDL/SDL_ttf.h>
#endif

SDL_VideoDevice*					CELL_VideoDevice;

/* Functions */
void								CELL_RC_SetFile				(const char* aPath);
void								CELL_RC_GetData				();
void								CELL_RC_DumpData			();

void								CELL_CAPTION_Set			(_THIS, const char* aCaption, const char* aIcon);
void								CELL_CAPTION_Draw			();

void								CELL_PSGL_Setup				(_THIS);
void								CELL_PSGL_Kill				(_THIS);
void								CELL_PSGL_Update			(_THIS);
int									CELL_PSGL_SetMode			(_THIS, SDL_Surface* aCurrent);

void				CELL_PSGL_MapTextureAsFrameBuffer			(GLuint aTextureID, uint32_t aWidth, uint32_t aHeight);
void				CELL_PSGL_UnmapTextureAsFrameBuffer			();
void								CELL_PSGL_SetUnderscan		(int32_t aX, int32_t aY);
void								CELL_PSGL_GetUnderscan		(int32_t* aX, int32_t* aY);

#ifdef CELL_SHADER_SUPPORT
void								CELL_SHADE_Load				(const char* filename, uint32_t aPass);
#endif
void								CELL_SHADE_UpdateMode		();
void								CELL_SHADE_Present			();

#ifndef CELL_NO_SOFT_INPUT
void								CELL_PAD_SetMode			(uint32_t aIndex, enum PadMode aMode);
void								CELL_PAD_ProcessButton		(uint32_t aButton, uint32_t aNew, uint32_t aOld);

void								CELL_PAD_SetFixedMap		(const CELL_FixedMap* aMap);

void								CELL_PAD_MOUSE_Enter		(enum PadMode aOldMode);
void								CELL_PAD_MOUSE_Exit			(enum PadMode aNewMode);
void FASTCALL						CELL_PAD_MOUSE_Axes			(int32_t aXAxis, int32_t aYAxis);
void FASTCALL 						CELL_PAD_MOUSE_Buttons		(uint32_t aNewButtons, uint32_t aOldButtons);

void								CELL_PAD_KEYBOARD_Enter		(enum PadMode aOldMode);
void								CELL_PAD_KEYBOARD_Exit		(enum PadMode aNewMode);
void FASTCALL 						CELL_PAD_KEYBOARD_Buttons	(uint32_t aNewButtons, uint32_t aOldButtons);
void FASTCALL 						CELL_PAD_KEYBOARD_Axes		(int32_t aXAxis, int32_t aYAxis);
enum HID_ID							CELL_PAD_KEYBOARD_GetMap	(uint32_t aButton);
void								CELL_PAD_KEYBOARD_Map		(uint32_t aButton, enum HID_ID aID);
void								CELL_PAD_KEYBOARD_Draw		();

void								CELL_PAD_JOYSTICK_Enter		(enum PadMode aOldMode);
void								CELL_PAD_JOYSTICK_Exit		(enum PadMode aNewMode);

#endif

void								CELL_PAD_JOYSTICK_Process	(SDL_Joystick* aJoystick, uint32_t aIndex);

void								CELL_PSGL_SetVertex			(GLfloat* aBase, float aX, float aY, float aU, float aV);
void								CELL_PSGL_SetRectangle		(GLfloat* aBase, float aX, float aY, float aWidth, float aHeight);
GLuint								CELL_PSGL_MakeTexture		();
void								CELL_PSGL_MakeAttribSetRect	(GLuint* aBufferID, GLuint* aAttributeSet, float aX, float aY, float aWidth, float aHeight);
void								CELL_PSGL_MakeAttribSetRectI(GLuint* aBufferID, GLuint* aAttributeSet, float aX, float aY, float aWidth, float aHeight);
void								CELL_PSGL_KillAttribSetRect	(GLuint* aBufferID, GLuint* aAttributeSet);

WMcursor*							CELL_CURSOR_Create			(_THIS, Uint8 *data, Uint8 *mask, int w, int h, int hot_x, int hot_y);
void								CELL_CURSOR_Free			(_THIS, WMcursor *cursor);
int									CELL_CURSOR_Show			(_THIS, WMcursor *cursor);
void 								CELL_CURSOR_Move			(_THIS, int x, int y);
uint32_t							CELL_CURSOR_Draw			(WMcursor* aCursor, int32_t aX, int32_t aY, uint32_t aScale);


void								CELL_PAD_Init				();
void								CELL_PAD_SetOpen			(SDL_Joystick* aJoystick, uint32_t aIndex, uint32_t aState);
int									CELL_PAD_DrawOverlay		();
void								CELL_PAD_JOYSTICK_Process	(SDL_Joystick* aJoystick, uint32_t aIndex);
void								CELL_PAD_ProcessByMode		();

void								CELL_MOUSE_Process			();
void								CELL_KEYBOARD_Process		();

void								CELL_EVENTS_Pump			(_THIS);
void								CELL_EVENTS_Init			(_THIS);

/* Private display data */
struct SDL_PrivateVideoData
{
	struct
	{
		PSGLdevice*					Device;				/* PSGL Device */
		PSGLcontext*				Context;			/* PSGL Context */

#ifdef CELL_SHADER_SUPPORT
		CGcontext					ShaderContext;		/* CG Context */
#endif

		GLuint						TextureBufferID;	/* Buffer that holds the screen pixels */
		GLuint						FrameBufferID;		/* Frame buffer object used for hw fills and two pass textures */

		uint32_t					RefreshTime;		/* The SDL_GetTicks time of the last screen update */
	}	PSGLState;

#ifdef CELL_SHADER_SUPPORT
	struct
	{
		uint32_t					Passes;
		uint32_t					PresentCount;
	}	ShaderData;
#endif


	struct
	{
		GLuint						TextureID;			/* Texture ID for this pass */
		GLuint						VertexBufferID;		/* Vertex Buffer for this pass */
		GLuint						AttributeSetID;		/* AttributeSet for this pass */

#ifdef CELL_SHADER_SUPPORT
		uint32_t					ScaleFactor;		/* The amount the texture for the next pass should be scaled up to */
		uint32_t					Smooth;				/* Enable Linear filtering for this pass */

		CGprogram					VertexProgram;		/* Vertex program ID for this pass */
		CGprogram					FragmentProgram;	/* Fragment program ID for this pass */

		CGparameter					Projection;
		CGparameter					FragmentVideoSize;
		CGparameter					FragmentTextureSize;
		CGparameter					FragmentOutputSize;
		CGparameter					FragmentFrameCount;
		CGparameter					VertexVideoSize;
		CGparameter					VertexTextureSize;
		CGparameter					VertexOutputSize;
		CGparameter					VertexFrameCount;
#endif
	}	Shader[2];

	struct
	{
		volatile uint32_t			WantToDie;			/* Quit game chosen from XMB */
		volatile uint32_t			WantToSleep;		/* XMB open */
		uint32_t					LastPump;			/* The SDL_GetTicks time of the last event update */
	}	Events;

	struct
	{
		uint32_t					Width;				/* Physical width of the screen */
		uint32_t					Height;				/* Physical height of the screen */
		GLfloat						AspectRatio;		/* Physical aspect ratio of the screen */

		GLint						Viewport[4];		/* X, Y, W, H */
		int32_t						UnderscanX;			/* Percent to underscan screen width */
		int32_t						UnderscanY;			/* Percent to underscan screen height */
	}	ScreenConfiguration;

	struct
	{
		uint32_t					Width;				/* SDL Video mode width */
		uint32_t					Height;				/* SDL Video mode height */
		uint32_t					BitsPerPixel;		/* SDL Video mode bit depth */
	}	VideoMode;

	struct
	{
		WMcursor*					Current;			/* Current mouse cursor */
		uint32_t					X;					/* Cursor X Position */
		uint32_t					Y;					/* Cursor Y Position */
	}	Cursor;

	struct
	{
		uint32_t					Have;
		uint32_t					Check;

		uint32_t					Buttons;
	}	Mouse;

	struct
	{
		uint32_t					Have;
		uint32_t					Check;

		uint32_t					Modifiers;
		uint32_t					Leds;
		uint8_t						KeyState[PS3_MAX_KEYS];
	}	Keyboard;

	struct
	{
		CELL_Pad					Pads[NUM_PADS];
		CELL_PadButton				ButtonMap[NUM_BUTTONS];

#ifndef CELL_NO_SOFT_INPUT
		CELL_OverlayImage			InputIcons[3];			/* 0: Joystick, 1: Mouse, 2: Keyboard */
		uint32_t					InputIconHideTime;
#endif
	}	Pads;

#ifndef CELL_NO_SOFT_INPUT
	struct
	{
		uint32_t					MappingOverride;

		WMcursor*					Cursor;
		int32_t						CursorX;
		int32_t						CursorY;

		const CELL_OSK_Key*			MappingMode;
		const CELL_OSK_Key*			Selected;
		const CELL_OSK_Key*			Down;

		CELL_OverlayImage			Image;
	}	OSK;
#endif

#ifdef CELL_OSK_CAPTION
	struct
	{
		TTF_Font*					Font;
		CELL_OverlayImage			Image;

		char						Caption[256];
		char						Icon[256];
		uint32_t					Update;
	}	Caption;
#endif
};

extern struct SDL_PrivateVideoData	CELL_Video;

#endif /* _SDL_cellvideo_h */

